<?php
$this->load->view('includes/header');
define('YT_API_URL', 'http://gdata.youtube.com/feeds/api/videos/');

?>
<!--<script src="<?php echo base_url();?>public/js/lp_profile.js"></script>-->

<section id="LeapMainWrapper" class="profileWrapper">
 
   
     
    <div class="homeWrapper <?php if(!$this->input->cookie('LEAPSESSIF')){ ?> mainHomeWrapper <?php } ?>" >
        <?php 
//        for ($i = 0; $i < 2; $i++) {
            shuffle($media);
            foreach ($media as $key => $value) {
                if(isset($value['body'])){
                    echo '<a class="mainBlock article" id="'.$value['_id'].'" href="#" >
                                <img class="desaturate" alt="" src="'.$value['thumb'].'"><i></i>
                                <div class="mediaInfos">
                                    <span class="videoUser"><img width="30px" height="30px" src='.$value['user_thumb'].' />'.$value['user_name'].'</span>
                                    <strong>'.$value['title'].'</strong>
                                    <span class="comment">'.$value['desc'].'</span>
                                    <em class="timestamp">'.time_since($value['timestamp']).'</em>
                                    <div class="articleBody">
                                        '.$value['body'].'
                                    </div>
                                </div>
                            </a>';
                }
                elseif(isset($value['video_id'])){
                    echo '<a class="mainBlock videoPost" id="'.$value['video_id'].'" href="#" >
                                <img class="desaturate" alt="" src="'.str_replace("hqdefault", "maxresdefault", $value['thumb']).'"><i></i>
                                <div class="mediaInfos">';
                                   echo getYoutubeInfos( $value['video_id']);
                                echo '</div>
                            </a>';
                }
                elseif (strpos($value['profile_pic'], 'graph.facebook.com/') !== false){
                    echo "<a id='" . $value['user_id'] . "' class='mainBlock' href='#' data-country='" . $value['country'] . "' data-city='" . $value['city'] . "' data-team='" . $value['team'] . "'><img class='desaturate scalable'  width='250' src='" . $value['profile_pic_thumb'] . "?height=250&width=250' />";
                        echo '<div class="mediaInfos">
                                    <span class="mediaTitle">'. $value['first_name'] ." ". $value['last_name'] .'</span>
                                    <span class="mediaInfo">'.$value['country'].'</span>
                                </div>';
                        echo "<div class='profileInfo'>
                            <div class='triangle arrow-up'></div>
                            <div class='personalInfos'><h4>". $value['first_name'] ." ". $value['last_name'] ."</h4><span><strong>Team</strong>: ". $value['team'] ."<br><strong>Email</strong>: ". $value['email_address'] ."<br><strong>Website</strong>: ". $value['website'] ."</span><p>";
                              if($value['mood'] !== "") echo " “". $value['mood'] ."”";
                           echo "</p></div>
                               <div class='moreInfos'>
                                    <h4>". $value['city'] ." ". (($value['country'] !== "") ? "".$value['country'] : "") ."</h4>
                               </div>
                        </div></a>";
                }
                elseif (strpos($value['profile_pic_thumb'], 'twimg.com/profile_images') !== false){
                    echo "<a id='" . $value['user_id'] . "' class='mainBlock' href='#' data-country='" . $value['country'] . "' data-city='" . $value['city'] . "' data-team='" . $value['team'] . "'><img class='desaturate scalable'  width='250' src='" . str_replace("_normal", "", $value['profile_pic_thumb']) . "?height=250&width=250' />";
                     echo '<div class="mediaInfos">
                                    <span class="mediaTitle">'. $value['first_name'] ." ". $value['last_name'] .'</span>
                                    <span class="mediaInfo">'.$value['country'].'</span>
                                </div>';   
                    echo " <div class='profileInfo'>
                            <div class='triangle arrow-up'></div>
                            <div class='personalInfos'><h4>". $value['first_name'] ." ". $value['last_name'] ."</h4><span><strong>Team</strong>: ". $value['team'] ."<br><strong>Email</strong>: ". $value['email_address'] ."<br><strong>Website</strong>: ". $value['website'] ."</span><p>";
                              if($value['mood'] !== "") echo "“". $value['mood'] ."”";
                           echo "</p></div>
                               <div class='moreInfos'>
                                    <h4>". $value['city'] ." ". (($value['country'] !== "") ? "".$value['country'] : "") ."</h4>
                               </div>
                        </div></a>";
                }
                else{
                    echo "<a id='" . $value['user_id'] . "' class='mainBlock' href='#' data-country='" . $value['country'] . "' data-city='" . $value['city'] . "' data-team='" . $value['team'] . "'><img class='desaturate scalable'  width='250' src='" . $value['profile_pic'] . "' />";
                     echo '<div class="mediaInfos">
                                    <span class="mediaTitle">'. $value['first_name'] ." ". $value['last_name'] .'</span>
                                    <span class="mediaInfo">'.$value['country'].'</span>
                                </div>';    
                    echo "<div class='profileInfo'>
                            <div class='triangle arrow-up'></div>
                            <div class='personalInfos'><h4>". $value['first_name'] ." ". $value['last_name'] ."</h4><span><strong>Team</strong>: ". $value['team'] ."<br><strong>Email</strong>: ". $value['email_address'] ."<br><strong>Website</strong>: ". $value['website'] ."</span><p>";
                              if($value['mood'] !== "") echo " “". $value['mood'] ."”";
                           echo "</p></div>
                               <div class='moreInfos'>
                                    <h4>". $value['city'] ." ". (($value['country'] !== "") ? "".$value['country'] : "") ."</h4>
                               </div>
                        </div></a>";
                }
//            }
        }
        ?>
    </div>
<!--    <div class="loadMoreWrapper">
        <button class="loadMore">LOAD MORE</button>
    </div>-->
</section>
<div class="overlay_bg"></div>

<div id="shotView" class="">
        <div id="shotContaier">
            <a id="shotUser" href="#">
                <span id="videoTitle"></span>
            </a>
            
            <a target="_blank" href="#" id="imageBox">
                <span class="imageholder">
                    <img src="" id="imageContainer">
                </span>
                <i></i>
            </a>
            <div id="infoBox">
                <span id="shotLikes" class="infoBlock" ><span>likes</span></span><span id="shotComments" class="infoBlock"><span>Time</span></span><span id="shotViews" class="infoBlock"><span>views</span></span>
            </div>
            <div id="commentBlock"><div class="comment" style="display: block;"></div></div>
        </div>
</div>
<div id="articleView" class="">
        <div id="shotContaier">
            <div id="shotUser" href="#">
                <span id="videoTitle"></span>
            </div>
            
            <a target="_blank" href="#" id="imageBox">
                <span class="imageholder">
                    <img src="" id="imageContainer">
                </span>
                <i></i>
            </a>
            <div id="infoBox">
                <span id="shotLikes" class="infoBlock" ><span>likes</span></span><span id="shotComments" class="infoBlock"><span>Time</span></span><span id="shotViews" class="infoBlock"><span>views</span></span>
            </div>
            <div id="commentBlock"><div class="comment" style="display: block;"></div></div>
        </div>
</div>
<div id="blogBox" class="">
    <div class="blogBox">
        <div class="addBlogHeader">
            <h4 style="">Add a Blog Entry:</h4>
            
        </div>
        <div class="blogTitle">
        <label>Title</label>
        <input type="text" id="blogtitle" maxlength="40"/>

        </div>
        <div class="shortDesc" >
            <label>Short Description</label>
            <input type="text" id="shortdesc" maxlength="250" />
        </div>
        <div class="overlay_content_main">
            <div class="progress">
                <div class="bar"></div>
            </div>
            <div class='preview'>
                <input type="hidden" id="articlethumb" name="articlethumb"/>
            </div>
            <form id="profilePicform" method="post" enctype="multipart/form-data" action='/profile/upload_picture'>
                <div class="photoimg_upload">
                    <span class="button">Upload Picture</span>
                    <input type="file" name="photoProfile" id="photoProfile" class="uploadPic"/>
                </div>
            </form>
        </div>
        <textarea id="redactor"></textarea>
        <div class="submitBlog">
            <button id="submitBlog" class="button">Publish</button>
        </div>
    </div>
    </div>
<script type="text/javascript" >
    $.lp.init();
    <?php if(!isset($user_id) || $user_id == ""){?>
    $.lp.register();
    <?php  }  ?>
    $.lp.uploadPicture;
    $.lp.userId = <?php if(isset($user_id) && $user_id != ""){ echo $user_id; } else echo "''"; ?>;
</script>

<?php
$this->load->view('includes/footer');

function getYoutubeInfos($video_id) {

//Change below the video id.

//Using cURL php extension to make the request to youtube API
    

//Using SimpleXML to parse youtube's feed
    $json = file_get_contents( YT_API_URL . $video_id."?v=2&alt=json");
    $entry = json_decode($json);
//If no entry whas found, then youtube didn't find any video with specified id
    if (!$json)
        exit('Error: no video with id "' . $video_id . '" whas found. Please specify the id of a existing video.');
    
    $views  = $entry->entry->{'yt$statistics'}->viewCount;
    $favoriteCount  = $entry->entry->{'yt$statistics'}->favoriteCount;
    $likes  = $entry->entry->{'yt$rating'}->numLikes;
    $dislike = $entry->entry->{'yt$rating'}->numDislikes;
    $author = $entry->entry->author[0]->name->{'$t'};
    $title = $entry->entry->title->{'$t'};
    return '<span class="mediaTitle">'. substr($title, 0, 25) .'</span>
        <span class="mediaInfo">'.$author.'</span>
            
            <span class="mediaViews"><i></i>'.$views.'</span>
            <div class="mediaLikesWrapper"><span class="mediaLikes"><i></i>'.$likes.'</span><span class="mediaDislikes"><i></i>'.$dislike.'</span></div>';

  
}

?>
